//
//  HomeFunctionCell.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/6/22.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "HomeFunctionCell.h"
#import "FunctionChildFirstCell.h"
#import "FunctionChildSecondCell.h"
#import "ExchangeIntegralLayout.h"


@interface HomeFunctionCell ()<UICollectionViewDelegate,UICollectionViewDataSource,IntegralFallLayoutDeleaget>

@property (nonatomic,strong) UIView *integralBackgroundView;
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation HomeFunctionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.selectionStyle =  UITableViewCellSelectionStyleNone;
        self.backgroundColor  = kAppBackgoundColor;
        [self.contentView addSubview:self.integralBackgroundView];
        [self.integralBackgroundView addSubview:self.collectionView];
        [self.integralBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView);
            make.left.mas_equalTo(self.contentView).mas_offset(kMarginOffsetX);
            make.right.mas_equalTo(self.contentView).mas_offset(-kMarginOffsetX);
            make.height.mas_offset(265);
        }];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.integralBackgroundView).mas_offset(5);
            make.left.right.mas_equalTo(self.integralBackgroundView);
            make.height.mas_offset(265);
        }];
        
    }
    return self;
}


- (void)updateCellData{
    [self.collectionView reloadData];
}





- (UIView *)integralBackgroundView{
    if (!_integralBackgroundView) {
        _integralBackgroundView = [[UIView alloc]init];
        _integralBackgroundView.backgroundColor = [UIColor whiteColor];
        _integralBackgroundView.layer.cornerRadius = 4;
        _integralBackgroundView.layer.shadowColor = [UIColor colorWithHexString:@"#999999"].CGColor;//shadowColor阴影颜色
        _integralBackgroundView.layer.shadowOffset = CGSizeMake(3,3);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3)
        _integralBackgroundView.layer.shadowOpacity = 0.6;//阴影透明度，默认0
        _integralBackgroundView.layer.shadowRadius = 3;//阴影半径，默认3
    }
    return _integralBackgroundView;
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        ExchangeIntegralLayout *layout = [[ExchangeIntegralLayout alloc] init];
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.scrollEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.layer.masksToBounds = YES;
        _collectionView.layer.cornerRadius = 4;
        _collectionView.tag = 100;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[FunctionChildFirstCell class] forCellWithReuseIdentifier:@"FunctionChildFirstCellIdentifier"];
        [_collectionView registerClass:[FunctionChildSecondCell class] forCellWithReuseIdentifier:@"FunctionChildSecondCellIdentifier"];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  7;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
       FunctionChildFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FunctionChildFirstCellIdentifier" forIndexPath:indexPath];
        cell.backgroundColor = kBlackColor;

        return cell;
    }else{
       FunctionChildSecondCell  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FunctionChildSecondCellIdentifier" forIndexPath:indexPath];
        cell.backgroundColor = kBlueColor;
        
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if([self.delegate respondsToSelector:@selector(selectFunctionCellItemIdex:)]){
        [self.delegate selectFunctionCellItemIdex:indexPath.row];
    }
    
}


#pragma mark  -  IntegralFallLayoutDeleaget

- (CGFloat)layout:(ExchangeIntegralLayout *)waterFallLayout
  heightForItemAtIndexPath:(NSUInteger)indexPath
                 itemWidth:(CGFloat)itemWidth{
    if (indexPath == 0 || indexPath == 3 ||  indexPath == 5 ) {
        return 80;
    }else{
        return 60;
    }
}

- (CGFloat)rowMarginInLayout:(ExchangeIntegralLayout *)waterFallLayout{
    return 2;
}

- (NSUInteger)columnCountInLayout:(ExchangeIntegralLayout *)waterFallLayout{
    return 2;
}


+ (CGFloat)heightForCell{
    return 275;
}



@end
